const map = new WeakMap();

const ob = new ResizeObserver((entries) => {
  for (const entry of entries) {
    // 运行回调
    const handler = map.get(entry.target);
    console.log(entry);
    handler &&
      handler({
        width: entry.borderBoxSize[0].inlineSize,
        height: entry.borderBoxSize[0].blockSize,
      });
  }
});

export default {
  mounted(el, bindings) {
    ob.observe(el); // 监听元素尺寸变化
    map.set(el, bindings.value);
  },
  unmounted(el) {
    ob.unobserve(el); // 取消监听
  },
};
